//
//  RCUserAccount.swift
//  RCWeibo
//
//  Created by CL on 15/12/16.
//  Copyright © 2015年 CL. All rights reserved.
//

import UIKit

class RCUserAccount: NSObject, NSCoding {

 /// 用户token
    var access_token: String?
 /// 授权时间
    var expires_in: NSTimeInterval = 0{
            didSet{
                
                expires_date = NSDate(timeIntervalSinceNow: expires_in)
                print(expires_date)
                
        }
    }
    /// token 到过期时间，开发者为5年，普通用户为3天
    var expires_date: NSDate?
 /// 用户id
    var uid: String?
 /// 用户名字
    var name: String?
 /// 用户头像
    var avatar_large: String?
    
    
    
    //MARK:
    //MARK:存取自定义对象
    //保存到沙盒
    func saveToSandbox(){
    
        //⚠️这里是 NSString
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //保存自定义对象
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    //从沙盒中取
    //加class表示类方法
    class func loadFromSandbox() -> RCUserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? RCUserAccount{
        
            //在这里应该判断是否过期,比较方法， 比较结果为 前者比后者大Descending
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            
                return account
            }
            
            return nil
            
         
        }
        
        print("沙盒并没有用户数据")
        return nil
        
    }
    
    
    //MARK:
    //MARK:kvc赋值
    init(dict: [String : AnyObject]){
    
        super.init()
        //KVC赋值
        self.setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        //为没有定义属性的key防报错
        
    }
    
    
    //MARK:
    //MARK:归档&解归档
    //解归档
    required init?(coder aDecoder: NSCoder) {
        //用？还是！要看具体定义时候是啥
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        //如果基本数据类型 就可以直接获取到具体类型的熟悉 不需要类型转换
        expires_in   = aDecoder.decodeDoubleForKey("expires_in")
        uid          = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name         = aDecoder.decodeObjectForKey("name") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate

    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
    }

    //MARK:
    //MARK:    重写描述属性
    override var description: String{
    
        let keys = ["access_token", "expires_in", "uid", "avatar_large", "name", "expires_date"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    
    
    
}
